import {Module} from "../framework.js"
import * as WidgetEvent from './widget-event.js';
import D from "../delegator.js"
import * as UI from '../ui.js'

export default class WidgetSelector extends Module {
    constructor(disp) {
        super(disp);
        this._ui = UI.UISelector();
        this._offset = -1;
        this._ui.closeBtn.onclick = _ => {
            this._ui.Hide();
        }

        this._ui.element.onclick = e => {
            D(e.target);
            while(D.HasTarget()) {
                if(D.HasAttribute('selector-type')) {
                    const _type = Number(D.GetAttribute('selector-type'));
                    this.Dispatch(WidgetEvent.WidgetSelectorItemClickedEvent(_type, this._offset));
                    this.Hide();
                    break;
                }
                D.SetParentAsTarget();
            }
        }
    }

    Show(offset) {
        this._offset = offset;
        this._ui.Show();
    }

    Hide(immediate = false) {
        this._ui.Hide();
    }
}

